import sys
import time
from pathlib import Path

# 将项目根目录（practice文件夹）加入Python搜索路径
sys.path.append(str(Path(__file__).parent.parent))

from selenium.webdriver.common.by import By

from page_objects.base_page import BasePage
from selenium.webdriver.support import expected_conditions as EC

class CategoryListPage(BasePage):
    __ADD = (By.XPATH, '//span[contains(text(),"添加")]')
    __DELETE = (By.XPATH, '//span[contains(text(),"删除")]')
    __INPUT_UID = (By.XPATH, '//input[@placeholder="请输入商品编号"]')
    __INPUT_NAME = (By.XPATH, '//input[@placeholder="请输入商品名称"]')
    __SUCCESS = (By.XPATH, '//p[contains(text(),"成功")]')
    def check_add(self):
        """商品列表页面：点击添加"""
        # 点击“添加”按钮
        self.do_find(self.__ADD).click()
        # ==》创建商品页面
        from page_objects.category_create_page import CategoryCreatePage
        return CategoryCreatePage(self.driver)
    
    def check_delete(self, uid, name):
        """商品列表页面：点击删除"""
        # 输入商品名称和编号
        self.do_send_keys(uid,self.__INPUT_UID)
        self.do_send_keys(name,self.__INPUT_NAME)
        # 点击删除
        self.do_find(self.__DELETE).click()
        # ==》创建商品页面
        from page_objects.category_create_page import CategoryCreatePage
        return CategoryCreatePage(self.driver)

    def get_operate_result(self):
        """商品列表页面：获取操作结果"""
        # 获取冒泡消息文本
        text = self.wait_element_until_visible(self.__SUCCESS).text
        # ==》返回消息文本
        return text